// pages/login/login.js
import config from '../../config'
const API = config.api
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    radio: false,
  },
  changeRadio() {
    this.setData({
      radio: !this.data.radio
    })
  },
  // 授权登录
  getPhoneNumber(e) {
    // console.log(e);
    if (this.data.radio == true) {
      if (e.detail.errMsg == 'getPhoneNumber:ok') {
       setTimeout(()=>{
        app.login({
          encryptedData: e.detail.encryptedData,
          iv: e.detail.iv,
          carBrand: 0,
          code: this.data.code
        }).then(response => {
          console.log(this.data.applylist);
          if (!this.data.userId && this.data.applylist == '') {
            this.setData({
              isShowanimation: false,
              isLogin: false,
              isMask: true,
              isShowCode: true,
              userId: response.userId,
            })
          }
          if (!this.data.userId && !this.data.applylist) {
            this.setData({
              isMask: false,
              isShowCode: false,
              isLogin: false,
              isShowanimation: false,
              userId: response.userId,
            })
          }

          // let optSync = wx.getLaunchOptionsSync()
          // if (optSync.scene == '1011' || optSync.scene == "1012" || optSync.scene == '1013' || optSync.scene == '1065' || optSync.scene == '1167' || optSync.scene == '1168' || optSync.scene == '1169' || optSync.scene == '1181' || optSync.scene == '1194') {

          // } else {
          // this.setData({
          //     userId: response.userId,
          //     isLogin: false,
          //     isMask: false,
          // })
          // }
        }).catch(err => {
            this.getCode()
        })
       },1000)
      }
    }else{
      wx.showToast({
        title: '请先同意协议隐私和隐私协议',
        icon: 'none',
        image: '',
        duration: 3000
      })
    }


  },
  getCode() {
    wx.login({
      // 发送 res.code 到后台换取 openId, sessionKey, unionId
      success: loginRes => {
        console.log(loginRes)
        this.setData({
          code: loginRes.code
        })
      }
    })
  },
  toagreement() {
    wx.navigateTo({
      url: '/pages/agreement/agreement'
    })
  },
  openPrivacyContract() {
    wx.openPrivacyContract({
      success: res => {
        console.log('openPrivacyContract success', res)
      },
      fail: res => {
        console.error('openPrivacyContract fail', res)
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    this.getCode()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})